<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="基金名称">
        <el-input v-model="Fund.fundName" placeholder="请输入基金名称" style="width:400px" />
      </el-form-item>
      <el-form-item label="基金类型">
         <el-select v-model="Fund.fundType" placeholder="请选择基金类型">
          <el-option
            v-for="item in type"
            :key="item.value"
            :label="item.label"
            :value="item.value">
            <span style="float: left">{{ item.label }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
          </el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="基金名称缩写" >
        <el-input v-model="Fund.fundNameAbbr" placeholder="请输入基金名称缩写"  style="width:400px"/>
      </el-form-item>
      <el-form-item label="基金公司名称">
        <el-input v-model="Fund.fundCompanyName" placeholder="请输入基金公司名称"  style="width:400px"/>
      </el-form-item>
      <el-form-item label="基金策略">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 18}"
          placeholder="请输入基金策略"
          v-model="Fund.investStrategy">
        </el-input>
      </el-form-item>
      <el-form-item label="基金理念">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 18}"
          placeholder="请输入基金理念"
          v-model="Fund.investPhilosophy">
        </el-input>
      </el-form-item>
      <el-form-item label="风险等级">
        <el-select v-model="Fund.riskLevel" placeholder="请选择基金风险等级">
          <el-option
            v-for="item in risk"
            :key="item.value"
            :label="item.label"
            :value="item.value">
            <span style="float: left">{{ item.label }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="购买状态">
        <el-select v-model="Fund.saleStatus" placeholder="请选择基金购买状态">
          <el-option
            v-for="item in saleStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value">
            <span style="float: left">{{ item.label }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="赎回基础费率"  style="width:200px">
        <el-input v-model="Fund.sellRate" placeholder="请输入基金赎回基础费率"  style="width:200 px"/>
      </el-form-item>
      <el-form-item label="管理费率"  style="width:200px">
        <el-input v-model="Fund.trusteeRate" placeholder="请输入基金管理费率"  style="width:200 px"/>
      </el-form-item>
      <el-form-item label="买入费率"  style="width:200px">
        <el-input v-model="Fund.manageRate" placeholder="请输入买入费率"  style="width:200 px"/>
      </el-form-item>
      <el-form-item label="投资策略">
       <el-select v-model="Fund.policy" placeholder="请选择基金购买状态">
          <el-option
            v-for="item in policy"
            :key="item.value"
            :label="item.label"
            :value="item.value">
            <span style="float: left">{{ item.label }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
          </el-option>
        </el-select>
      </el-form-item>
     
      
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import FundAPI from "@/api/fund";
import ImageCropper from "@/components/ImageCropper";
import PanThumb from "@/components/PanThumb";

export default {
  components: { PanThumb, ImageCropper },
  data() {
      //定义变量和初始值
    return {
      Fund: {},
      saveBtnDisabled: false, // 保存按钮是否禁用,
      imagecropperShow: false, //头像上传组件是否显示
      imagecropperKey: 0,
      BASE_API: process.env.BASE_API, //获取dev.env.js里面的地址
      type: [{
          value: 'BLEND',
          label: '混合型'
        }, {
          value: 'STOCK',
          label: '股票型'
        }, {
          value: 'INDEX',
          label: '指数型'
        }, {
          value: 'BOND',
          label: '债券型'
        }, {
          value: 'CURRENCY',
          label: '货币型'
        }],
        risk: [{
          value: 'L',
          label: '低风险'
        }, {
          value: 'M',
          label: '中高风险'
        }, {
          value: 'H',
          label: '高风险'
        }],
        saleStatus: [{
          value: 'SUBSCRIBE',
          label: '认购期'
        }, {
          value: 'PURCHASE',
          label: '开放买卖'
        }, {
          value: 'OFF',
          label: '本平台暂不销售'
        }],
         policy: [{
          value: '稳健型',
          label: '稳健型'
        }, {
          value: '稳中取进型',
          label: '稳中取进型'
        }, {
          value: '激进型',
          label: '激进型'
        }, {
          value: '保守型',
          label: '保守型'
        }, {
          value: '绩优股基',
          label: '绩优股基'
        }],
        position: '',
    };
  },
  created() {
    //页面渲染之前执行
    this.init();
  },
  watch: {
    //监听
    $route(to, from) {
      //路由变化方式，路由发生变化，方法就会执行
      this.init();
    },
  },
  methods: {
    close() {
      //关闭上传头像组件
      this.imagecropperShow = false;
      //上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1;
    },
    cropSuccess(data) {
      //上传成功后返回的图片地址
      this.Teacher.pic = data.fileUrl;
      //上传头像成功
      this.imagecropperShow = false;
      //上传成功后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1;
    },
    init() {
      //判断路径有id值,做修改
      if (this.$route.params && this.$route.params.id) {
        //从路径获取id值
        const id = this.$route.params.id;
        //调用根据id查询的方法
        this.getInfo(id);
      } else {
        //路径没有id值，做添加
        //清空表单
        this.Fund = {};
      }
    },
    //根据基金id查询的方法
    getInfo(id) {
      FundAPI.selectOne(id).then((response) => {
        this.Fund = response.data.fundFullInfo;
      });
    },
    saveOrUpdate() {
      //判断修改还是添加
      //根据teacher是否有id
      if (!this.Fund.id) {
        //添加
        this.addFund();
      } else {
        //修改
        this.editFund();
      }
    },
    //修改教师信息的方法
    editFund() {
      FundAPI.updayteOne(this.Fund).then((response) => {
        //修改成功
        //提示信息
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        //回到基金管理页面 路由跳转
        this.$router.push({ path: "/list/fund" });
      });
    },
    //添加教师的方法
    addFund() {
      FundAPI.addOne(this.Fund).then((response) => {
        //添加成功
        //提示信息
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        //回到教师管理页面 路由跳转
        this.$router.push({ path: "/list/fund" });
      });
    },
  },
};
</script>
<style>
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
</style>
